'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.Order
  }

  // 匹配查询
  get searchKey() {
    return ['outTradeNo']
  }

  // 模糊查询
  get likeKey() {
    return []
  }

  // 列表查询前
  beforeIndex() {
    const { ctx } = this
    const { query, helper } = ctx
    const { admin } = ctx.state
    const filter = {}

    if (!admin.isAdmin) filter.adminId = admin.adminId

    return {
      filter,
      // sort: {
      //   isTop: 'DESC',
      //   rank: 'ASC',
      // },
    }
  }

  async beforeCreate() {
    const { ctx } = this
    const { helper } = ctx
    const { body } = ctx.request
    const { admin } = ctx.state

    body.orderNo = helper.getOrderNo()
    body.outTradeNo = body.orderNo
    body.adminId = admin.adminId
  }
}

module.exports = Controller
